﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;

namespace LM.CB.K3Cloud.Plugin
{
    /// <summary>
    /// 作业数量维护表单插件
    /// </summary>
    /// <remarks>
    /// 功能：点击按钮“获取辅助车间作业”后，从生产入库单获取工艺路线信息，如果是辅助车间则获取工艺路线的所有作业活动数据。
    /// 创建人：吴振金
    /// 创建日期：2024-02-19
    /// </remarks>
    [Description("获取辅助车间作业"), HotUpdate, Obsolete("作废", true)]
    public class ZYSLWHGetFZCJZY : AbstractBillPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);

            if (!e.BarItemKey.EqualsIgnoreCase("DEV_tbGETFZCJZY"))
            {
                return;
            }

            DynamicObject forgObj = Model.GetValue("FACCTGORGID") as DynamicObject;
            // 组织
            string forgid = Convert.ToString(forgObj["id"]);
            // 年度
            string year = Convert.ToString(View.Model.GetValue("FYEAR"));
            // 期间
            string period = Convert.ToString(View.Model.GetValue("FPERIOD"));
            //string sql = @"/*dialect*/exec DEV_GetFZCJZY @ORGID='" + forgid + "',@year='" + year + "',@month='" + period + "'";
            var sql = $@"/*dialect*/
				select t.F_DEV_CBZX, t.FWORKACTIVEID, t.FCOSTCENTERID, t.FENTRYID, sum(t.QTY) as 'QTY'
				from (
					select
						B.F_DEV_CBZX,-- 工艺路线（成本中心）
						B.FWORKACTIVEID,-- 成本中心（作业活动）
						A.FCOSTCENTERID,-- 受益成本中心编码
						( ISNULL( B.F_DEV_HYL, 0 ) / ISNULL( B.F_DEV_CPSL, 0 ) ) * ISNULL( B.F_DEV_PRICE, 0 ) * ISNULL( A.FREALQTY, 0 ) AS QTY,
						A.FENTRYID-- 生产订单
					from (
						select
							SUM(B.FREALQTY) FREALQTY,-- 生产入库实收数量
							C.FENTRYID,-- 生产订单编号
							B.F_DEV_GYLX,-- 生产入库单（工艺路线）
							D.FCOSTCENTERID -- 成本中心 受益成本中心编码
						from T_PRD_INSTOCK A
						left join T_PRD_INSTOCKENTRY B on A.FID = B.FID
						left join T_CB_PROORDERTYPE C on B.FMOBILLNO = C.FNUMBER
							and B.FMOENTRYID = C.FPROORDERENTRYID -- 生产订单 基础资料
						left join T_CB_COSTCENTER D on B.FWorkShopId = D.FRELATION
							and D.FRELATIONTYPE = 'BD_Department' -- 成本中心(生产入库单车间对应成本中心关联对象)
						where A.FSTOCKORGID = {forgid}-- 组织1011
							and YEAR (A.FDATE) = {year}-- 年度
							and MONTH (A.FDATE) = {period}-- 期间
						group by
							C.FENTRYID,
							B.F_DEV_GYLX,
							D.FCOSTCENTERID
					) A
					inner join (
						select
							A.FID,--工艺路线
							B.F_DEV_CBZX,--成本中心 输出成本中心编码
							C2.FWORKACTIVEID,--作业活动
							B.F_DEV_FYDW,--费用单位
							A.F_DEV_CPSL,--产品数量
							B.F_DEV_HYL,--耗用量
							D.F_DEV_PRICE --标准费率(单价)
						FROM DEV_t_ProcessRoute A --工艺路线
						LEFT JOIN DEV_t_ProcessRoute_E B ON A.FID= B.FID
						LEFT JOIN T_CB_COSTCENTER C ON B.F_DEV_CBZX= C.FCOSTCENTERID
							AND C.FRELATIONTYPE= 'BD_Department' --成本中心
						LEFT JOIN T_BD_DEPARTMENT C1 ON C.FRELATION= C1.FDEPTID --部门 部门属性FDEPTPROPERTY
						LEFT JOIN T_CB_COSTCENTERWA C2 ON C.FCOSTCENTERID= C2.FCOSTCENTERID --成本中心(作业活动)
						LEFT JOIN (
							SELECT B.F_DEV_CBZX, B.F_DEV_FYXMBM, B.F_DEV_PRICE
							FROM DEV_t_StandardRate A, DEV_t_StandardRate_E B
							WHERE A.FID= B.FID --AND A.F_DEV_YEAR='' AND F_DEV_MONTH='' AND F_DEV_USEORGID=''
						) D ON B.F_DEV_CBZX= D.F_DEV_CBZX AND B.F_DEV_FYXM= D.F_DEV_FYXMBM --标准费率
						-- 辅助生产部门	：d8b03c562ba941faace5e98ff6cc25ad；基本生产部门：4866f13a3a3940b9b2fe47895a6e7cbe
						WHERE C1.FDEPTPROPERTY = 'd8b03c562ba941faace5e98ff6cc25ad'
					) B ON A.F_DEV_GYLX= B.FID
				) t
				group by t.F_DEV_CBZX, t.FWORKACTIVEID, t.FCOSTCENTERID, t.FENTRYID;";
            DynamicObjectCollection dy = DBUtils.ExecuteDynamicObject(Context, sql.ToString());
            if (dy.Count == 0)
            {
                return;
            }

            View.Model.DeleteEntryData("FEntity");
            //foreach (var dy1item in dy)
            for (int i = 0, l = dy.Count; i < l; i++)
            {
                var dy1item = dy[i];
                Model.CreateNewEntryRow("FEntity");

                View.Model.SetItemValueByID("FSRCCOSTCENTERID", Convert.ToString(dy1item["F_DEV_CBZX"]), dy.IndexOf(dy1item));// 输出成本中心

                View.Model.SetItemValueByID("FWORKACTIVEID", Convert.ToString(dy1item["FWORKACTIVEID"]), dy.IndexOf(dy1item));// 作业活动

                View.Model.SetItemValueByID("FDESCOSTCENTERID", Convert.ToDouble(dy1item["FCOSTCENTERID"]), dy.IndexOf(dy1item));// 受益成本中心 

                View.Model.SetValue("FQTY", Convert.ToDouble(dy1item["QTY"]), dy.IndexOf(dy1item));// 计价数量

                //View.Model.SetValue("FPROORDERTYPE", "PO", dy.IndexOf(dy1item));// 业务类型
                View.Model.SetValue("FPROORDERTYPE", "PO", i);// 业务类型

                View.Model.SetItemValueByID("FPROORDERID", Convert.ToString(dy1item["FENTRYID"]), dy.IndexOf(dy1item));// 订单编号
            }
            View.UpdateView("FEntity");
        }
    }
}
